import axios from "axios";
import Vue from "vue";


axios.defaults.timeout = 100000;
axios.defaults.baseURL = ''





const CancelToken = axios.CancelToken;

// function getQueryString(name, search) {
//     search =
//         search ||
//         window.location.search.substr(1) ||
//         window.location.hash.split("?")[1];
//     if (search) {
//         let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
//         let r = search.match(reg);
//         if (r != null) return unescape(r[2]);
//         return null;
//     } else {
//         return null;
//     }
// }
// http request 请求拦截器，有token值则配置上token值
axios.interceptors.request.use(
    (config) => {
        if (config.cancelTokenName) {
            config.cancelToken = new CancelToken(function executor(c) {
                Vue.prototype.$httpRequestList[config.cancelTokenName] ?
                    Vue.prototype.$httpRequestList[config.cancelTokenName]() :
                    "";
                Vue.prototype.$httpRequestList[config.cancelTokenName] = c;
            });
        } else if (config.url.indexOf("/ChainApi") != -1) {
            config.baseURL = '/'
        }

        return config;
    },
    (err) => {
        return Promise.reject(err); //请求错误时，直接结束
    }
);

// http response 服务器响应拦截器，这里拦截401错误，并重新跳入登页重新获取token
axios.interceptors.response.use(
    (response) => {
        if (response.status == 200 || response.status == 201) {
            return response.data;
        }
    },
    (error) => {
        if (error.response) {
            let err = {
                message: "",
            };
            switch (error.response.status) {
                case 400:
                    err.message = "请求错误(400)";
                    break;
                case 401:
                    // 这里写清除token的代码
                    err.message = "未授权，请重新登录(401)";
                    // router.replace({
                    //     path: "login",
                    //     query: {
                    //         redirect: router.currentRoute.fullPath,
                    //     }, //登录成功后跳入浏览的当前页面
                    // });

                    break;
                case 403:
                    err.message = "拒绝访问(403)";
                    break;
                case 404:
                    err.message = `请求出错(404)`;
                    break;
                case 408:
                    err.message = "请求超时(408)";
                    break;
                case 450:
                    err.message = "登陆超时,请重新登陆";
                    setTimeout(() => {
                        window.location.href = "login.html#/login";
                    }, 1500);
                    break;
                case 500:
                    err.message = "服务器错误(500)";
                    break;
                case 501:
                    err.message = "服务未实现(501)";
                    break;
                case 502:
                    err.message = "网络错误(502)";
                    break;
                case 503:
                    err.message = "服务不可用(503)";
                    break;
                case 504:
                    err.message = "网络超时(504)";
                    break;
                case 505:
                    err.message = "HTTP版本不受支持(505)";
                    break;
                default:
                    err.message = `连接出错(${err.response.status})!`;
            }
            // Notification.error({
            //     title: "错误",
            //     message: err.message,
            // });
        }
        return Promise.reject(error); // 请求错误时， 直接结束

        //return Promise.resolve(error);//请求错误时，不会直接结束，将会继续传到then里面,无论请求成功还是失败，在成功的回调中都能收到通知
    }
);

export default axios;